<?php

use Illuminate\Database\Capsule\Manager as DB;

/**
 * @name 认证管理
 */
class AuditController extends ACommonController
{
    protected $table = 'company_info';
    protected $primaryKey = 'company_id';
    protected $condition = [];

    /**
     * @name 公司待认证记录
     */
    public function companyAction(){
        if($this->isPost()){
            $handle = DB::table($this->table)->where('audit_company', 1);
            $keywords = $this->get('keywords');
            if (!empty($keywords)) {
                $handle = $handle->where('full_name', 'like', "%{$keywords}%");
            }
            $total  = $handle->count();
            $rows   = $handle->get();
            if ($rows->isNotEmpty()) {
                foreach ($rows as $index => $row) {
                    $row->subject = DB::table('company')->where('id', $row->company_id)->first();
                }
            }
            json(['total' => $total, 'rows' => $rows]);
        }else {
            $this->_view->assign('uniqid', uniqid());
            $this->_view->display(strtolower($this->controller) . '/company.html');
        }
    }

    /**
     * @name 会员待认证记录
     */
    public function membersAction(){
        if($this->isPost()) {
            $handle = DB::table($this->table)->where('audit_members', 1);
            $keywords = $this->get('keywords');
            if (!empty($keywords)) {
                $handle = $handle->whereIn('company_id', DB::table('members_info')->where('realname', 'like', "%{$keywords}%")->pluck('company_id'));
            }
            $total  = $handle->count();
            $rows   = $handle->get();
            if ($rows->isNotEmpty()) {
                foreach ($rows as $index => $row) {
                    $row->subject = DB::table('members_info')->where('company_id', $row->company_id)->first();
                }
            }
            json(['total' => $total, 'rows' => $rows]);
        }else{
            $this->_view->assign('uniqid', uniqid());
            $this->_view->display(strtolower($this->controller) . '/members.html');
        }
    }

    /**
     * @name 企业状态
     */
    public function companystatusAction()
    {
        do {
            $rows = [
                'company_id'     => $this->get('id', 0),
                'audit_company'  => $this->get('status', 0),
            ];
            if($rows['audit_company']==3){
                $rows['company_reason'] = $this->get('reason');
            }
            if (DB::table($this->table)->where('company_id', $rows['company_id'])->update($rows) !== FALSE) {
                $result = [
                    'ret' => 0,
                    'msg' => '操作成功',
                ];
            } else {
                $result = [
                    'ret' => 2,
                    'msg' => '操作失败',
                ];
            }
        } while (FALSE);
        json($result);
    }

    /**
     * @name 会员状态
     */
    public function membersstatusAction()
    {
        do {
            $rows = [
                'company_id'     => $this->get('id', 0),
                'audit_members'  => $this->get('status', 0),
            ];
            if($rows['audit_members']==3){
                $rows['members_reason'] = $this->get('reason');
            }
            if (DB::table($this->table)->where('company_id', $rows['company_id'])->update($rows) !== FALSE) {
                $result = [
                    'ret' => 0,
                    'msg' => '操作成功',
                ];
            } else {
                $result = [
                    'ret' => 2,
                    'msg' => '操作失败',
                ];
            }
        } while (FALSE);
        json($result);
    }

    /**
     * @name 企业信息查看
     */
    public function companyviewAction()
    {
        $id = $this->get('id', 0);
        if (DB::table($this->table)->where($this->primaryKey, '=', $id)->doesntExist()) {
            ret(1, '记录不存在');
        }
        $query   = $this->model ? (new $this->model) : DB::table($this->table);
        $dataset = $query->where($this->primaryKey, '=', $id)->first();
        $this->_view->assign('dataset', $dataset);
        $this->_view->assign('company_name', DB::table('company')->where('id', $id)->value('name'));
        $this->_view->assign('uniqid', uniqid('mix'));
        $this->_view->display( strtolower($this->controller) . '/companyview.html');
    }

    /**
     * @name 会员信息查看
     */
    public function membersviewAction()
    {
        $id = $this->get('id', 0);
        if (DB::table($this->table)->where($this->primaryKey, '=', $id)->doesntExist()) {
            ret(1, '记录不存在');
        }
        $dataset = DB::table('members_info')->where('company_id', '=', $id)->first();
        $this->_view->assign('dataset', $dataset);
        $this->_view->assign('uniqid', uniqid('mix'));
        $this->_view->display( strtolower($this->controller) . '/membersview.html');
    }

    /**
     * @name 企业删除
     */
    public function deleteAction()
    {

    }

}
